package acm;

import java.util.Scanner;

public class a19{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        String[] result = new String[11];
        int n = in.nextInt();
        for (int i=0;i<n;i++){
            String h=in.next();
            StringBuilder tempB=new StringBuilder();
            for (int j = 0;j<h.length();j++){
                char m = h.charAt(j);
                String b = Integer.toBinaryString(Integer.valueOf(String.valueOf(m),16));
                for (int k=b.length();k<4;k++){
                    b='0'+b;
                }
                tempB.append(b);
            }
            StringBuilder temp0=new StringBuilder();
            int addzero = 3-tempB.length()%3;
            for (int k=0;k<addzero;k++){
                tempB=new StringBuilder("0").append(tempB);
            }
            for (int m=0;m<tempB.length();m+=3){
                String numB=tempB.substring(m,m+3);
                String o = Integer.toOctalString(Integer.valueOf(String.valueOf(numB),2));
                temp0.append(o);
            }
            result[i]=temp0.toString().replaceAll("(0+)","");
        }
        for (int i=0;i<n;i++){
            System.out.println(result[i]);
        }
    }
}
